package scau.mis4.chanyulin.view;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import scau.mis4.chanyulin.tools.utils.StrUtils;

/**
 * Created by cyl on 2017/4/7
 * 提示
 */
public class AlertStage extends Stage {

    public AlertStage(String msg) {

        // 提示文本
        Label label = new Label(msg);
        label.setWrapText(true);
        label.setFocusTraversable(false);

        HBox msgBox = new HBox(10);
        msgBox.setAlignment(Pos.CENTER);
        msgBox.setPadding(new Insets(0, 20, 0, 20));
        msgBox.getChildren().add(label);

        // 确定按钮
        Button ok = new Button("确定");
        ok.setPrefSize(64, 24);
        ok.setCursor(Cursor.HAND);
        ok.setFocusTraversable(false);
        ok.setOnAction(e -> {this.close();});

        HBox btBox = new HBox(12.5);
        btBox.setAlignment(Pos.CENTER);
        btBox.getChildren().addAll(ok);

        VBox vBox = new VBox(7.5);
        vBox.setAlignment(Pos.CENTER);
        vBox.setPadding(new Insets(-7.5, 0, 0, 0));
        vBox.getChildren().addAll(msgBox, btBox);

        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        hBox.setPrefSize(calculateWidth(msg), 75);
        hBox.getChildren().add(vBox);

        this.setTitle("提示");
        this.setResizable(false);
        this.initModality(Modality.APPLICATION_MODAL);
        this.setScene(new Scene(hBox));
        this.show();

    }

    /**
     * 根据字符串计算窗口的宽度
     */
    private double calculateWidth(String msg) {
        double width = 0;
        int length = msg.length();
        // 中文
        if (StrUtils.startWithChinese(msg)) {
            if (length >= 24)
                width = 325.0;
            else if (length <= 8)
                width = 130.0;
            else
                width = length * 16;
        }
        // 英文
        else {
            if (length >= 46)
                width = 325.0;
            else if (length <= 14)
                width = 120.0;
            else
                width = length * 9;
        }
        return width;
    }

}
